package com.stylefeng.guns.modular.hhtb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.modular.hhtb.model.*;
import com.stylefeng.guns.modular.hhtb.dao.UsersRechargeMapper;
import com.stylefeng.guns.modular.hhtb.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户充值表 服务实现类
 * </p>
 *
 * @author wudi
 * @since 2018-09-15
 */
@Service
public class UsersRechargeServiceImpl extends ServiceImpl<UsersRechargeMapper, UsersRecharge> implements IUsersRechargeService {
    @Autowired
    private UsersRechargeMapper usersRechargeMapper;
    @Autowired
    private ILogHhtbcService logHhtbcService;
    @Autowired
    private  IAccountService accountService;
    @Autowired
    private IHhtbusersService hhtbusersService;
    @Autowired
    private IConfigsService configsService;

    @Override
    public List<UsersRechargeDTO> seelctUsersRechargeAll(@Param("page")Page<UsersRechargeDTO> page,
                                                      @Param("usersRechargeDTO")UsersRechargeDTO usersRechargeDTO) {
        List<UsersRechargeDTO> list = usersRechargeMapper.seelctUsersRechargeAll(page,usersRechargeDTO);
        Integer total = usersRechargeMapper.selectUsersRechargeCount(usersRechargeDTO);
        page.setTotal(total);
        return list;
    }
    @Override
    @Transactional
    public Map<String,Object> updateUsersRecharge(UsersRecharge usersRecharge1){
        Map<String,Object> map=new HashMap<>();
        try{
            UsersRecharge  usersRecharge=null;
            Integer status = usersRecharge1.getStatus();
            if (status==1){
                if (usersRecharge1.getId()==null){
                    map.put("code","00");
                    map.put("msg","未找到充值记录");
                    return map;
                }
                usersRecharge = this.selectById(usersRecharge1.getId());
                Integer userId= usersRecharge.getUserId();
                if (userId==null){
                    map.put("code","00");
                    map.put("msg","未找到充值会员");
                    return map;
                }
                Account account =new Account();
                account.setUserId(userId);
                EntityWrapper<Account> entityWrapper =new EntityWrapper<Account>(account);
                Account account1 = accountService.selectOne(entityWrapper);
                BigDecimal preHHTBC=account1.getHhtbc();
                BigDecimal cash = usersRecharge.getCash();
                //获取费率
                Map<String,Object> parameterMap=new HashMap<>();
                parameterMap.put("fieldCode","hhtbRate");
                parameterMap.put("page","1");
                String selectValue = configsService.selectValue(parameterMap);
                BigDecimal hhtbRate=new BigDecimal(selectValue);
                BigDecimal hHTBC=cash.multiply(hhtbRate);
                //修改用户级别
                Hhtbusers hhtbusers = hhtbusersService.selectById(userId);
                if (hhtbusers.getUserStatus()==0){
                    map.put("code","00");
                    map.put("msg","该用户已锁定");
                    return map;
                }
                //生成HHTB流水
                String orderNo = usersRecharge1.getOrderNo();
                LogHhtbc logHhtbc = logHhtbcService.getLogHhtbc(userId, userId, orderNo, preHHTBC, hHTBC, 1, "充值现金转化成HHTB", 0);
                logHhtbcService.insert(logHhtbc);
                //修改账户HHTB
                account1.setHhtbc(account1.getHhtbc().add(hHTBC).setScale(4));
                accountService.updateById(account1);

                if (hhtbusers.getUserType()==0){
                    Integer userType=cash.intValue();
                    hhtbusers.setUserType(userType);
                    hhtbusersService.updateById(hhtbusers);
                }
                usersRecharge.setCheckRemark("");
            }
            usersRecharge.setRemark("现金充值");
            usersRecharge.setCheckStaffId(usersRecharge1.getCheckStaffId());
            usersRecharge.setStatus(status);
            Date date=new Date();
            usersRecharge.setCheckTime(date);
            this.updateById(usersRecharge);
            map.put("code","01");
            map.put("msg","审核成功");
            return map;
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code", "00");
            map.put("msg", "审核失败");
            return map;
        }
    }
}
